@extends('layout.default')

@section('layout.content')
  <div class="row">
    <div class="col-xs-12">
      <form method="post" class="form-horizontal" id="role-update" autocomplete="off">
        <input type="hidden" name="_method" value="put"/>
        {{ csrf_field() }}
        <div class="form-group">
          <label class="col-sm-2 control-label no-padding-right" for="name"> <b><span class="red">*</span> 角色名称：</b>
          </label>
          <div class="col-sm-4">
            <input type="text" class="form-control" id="name" name="name" placeholder="角色名称" value="{{$role->name}}"/>
          </div>
        </div>
        <div class="form-group">
          <label class="col-sm-2 control-label no-padding-right" for="name"> 权限：</label>
          <div class="col-sm-10">
            @foreach($permissions as $parent)
              <div class="checkbox">
                <label class="no-padding-left">
                  <span class="lbl"><b> {{$parent->title}} </b></span>
                </label>
                @foreach ($parent['children'] as $permission)
                  <label class="">
                    <input name="permission" type="checkbox" value="{{$permission->id}}" class="ace ace-checkbox-2"
                    @foreach($role->permissions as $role_permission)
                      {{ $role_permission->id == $permission->id ? 'checked':'' }}
                        @endforeach
                    />
                    <span class="lbl no-padding-left"> {{$permission->title}}</span>
                  </label>
                @endforeach
              </div>
            @endforeach
          </div>
        </div>


        <div class="form-group">
          <label class="col-sm-2 control-label no-padding-right"> </label>
          <div class="col-sm-4">
            <button type="button" class="btn btn-primary submit">
              <i class="ace-icon fa fa-check"></i>
              <span class="">确定</span>
            </button>
          </div>
        </div>

      </form>
    </div>
  </div>

@stop

@section('javascript')
  <script type="text/javascript">
      $(function () {

          $('.submit').on('click', function () {
              var permissions = new Array();
              $("input[name='permission']:checked").each(function () {
                  permissions.push($(this).val());
              });
              //console.log(permissions);
              var uri = '{{route('system.role.update', $role->id)}}';

              $.post(uri, {
                  '_token': $("input[name='_token']").attr('value'),
                  'name': $("input[name='name']").val(),
                  'permissions': permissions,
                  '_method': 'put',
                  'is_submit': true
              }, function (data) {
                  console.log(data);
                  layer.msg(data.message, {shift: -1}, function () {
                      if (data.status === true && data.url != null) {
                          $(window).attr('location', data.url);
                      }
                  });

              }, 'json').error(function (data) {
                  layer.msg(data.responseJSON.message);
              });

          });


      });
  </script>
@stop